package com.liohi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.net.HttpHeaders;
import com.liohi.Param.R;
import com.liohi.entity.SimulationEquipment;
import com.liohi.service.SimulationEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@CrossOrigin
@RestController
@Api(tags = "仿真设备")
@RequestMapping("simulation-equipment")
public class SimulationController {
    @Autowired
    private SimulationEquipmentService simulationEquipmentService;

    @ApiOperation("查询所有仿真设备")
    @GetMapping("list")
    public R getSimulationList(@NotNull HttpServletRequest httpServletRequest){
        List<SimulationEquipment> list = simulationEquipmentService.list();
        return R.ok("items",list);
    }

    @ApiOperation("添加仿真设备")
    @PostMapping("add")
    public R addSimulation(@RequestBody SimulationEquipment simulationEquipment){
        boolean saved = simulationEquipmentService.save(simulationEquipment);
        if(saved)return R.ok("添加成功");
        else return R.error("添加失败");
    }

    @ApiOperation("条件查询仿真设备")
    @PostMapping("list/page")
    public R listEquipmentByPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, String num, String name) {
        IPage<SimulationEquipment> page = simulationEquipmentService.listByPage(pageNum, pageSize, num, name);
        return R.ok("data",page);
    }

    @ApiOperation("删除仿真设备")
    @DeleteMapping ("remove/{id}")
    public R remove(@PathVariable Integer id){
        if(id == null)return R.error("参数错误");
        boolean flag = simulationEquipmentService.removeById(id);
        return flag?R.ok("删除成功"):R.error("删除设备失败");
    }

    @ApiOperation("根据Id查询仿真设备")
    @GetMapping("getById/{id}")
    public  R getById(@PathVariable Integer id){
        if(id==null)return R.error("参数错误");
        SimulationEquipment simulationEquipment = simulationEquipmentService.getById(id);
        return R.ok("data",simulationEquipment);
    }

    @ApiOperation("根据id修改仿真设备")
    @PutMapping("updateById/{id}")
    public R updateById(@RequestBody SimulationEquipment simulationEquipment){
        boolean flag = simulationEquipmentService.updateById(simulationEquipment);
        return flag? R.ok("修改成功"):R.error("修改失败");
    }

    @GetMapping("export")
    public void exportLeaveData(HttpServletResponse response) throws Exception {

        try{
            List<SimulationEquipment> equipmentList = simulationEquipmentService.list(); // 获取所有设备数据

            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("设备列表", "设备", ExcelType.XSSF), SimulationEquipment.class, equipmentList);

//            response.setContentType("applicationnd.ms-excel");
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("SimulationEquipment.xlsx", "UTF-8"));

            workbook.write(response.getOutputStream());
            workbook.close();
        }catch (IOException e) {
            e.printStackTrace();
        }

    }


}
